<?php
Class AdministracionSede  extends Seguridad{
	 public function index()
    {
    		
    	parent::chequear();
		if (isset($this->session->userdata['privilegio'])) {
    	$data['title'] = 'Administracion';
		$data['main_content'] = 'administracionGeneralSede';


		$this->load->model('administracion_model_sede');

		$data['sedes'] = $this->administracion_model_sede->leer_sedes();
		$this->load->view('main_template',$data);
		}
    }
    public function cargarImagen(){
     $config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '100';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('img'))
		{
			$error = array('error' => $this->upload->display_errors());
			print_r($error);
		}
		
		$fd = $this->upload->data();
		return $fd ;
    
    }
	public function insertar_sede(){
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('nomSede', 'Nombre de sede', 'required|min_length[3]|max_length[20]|is_unique[tb_sede.nombre]|alpha_dash|xss_clean');
		$this->form_validation->set_rules('dir', 'Direccion', 'required|min_length[3]|integer');
		$this->form_validation->set_rules('ubic', 'Ubicacion', 'required|min_length[3]');
//		$this->form_validation->set_rules('apellido', 'Apellido', 'required|min_length[3]|max_length[20]');
		$this->form_validation->set_rules('decrip', 'Descripcion', 'required|min_length[3]|max_length[20]|trim');
		
		$this->form_validation->set_message('integer', 'El campo %s debe poseer solo numeros enteros');
		$this->form_validation->set_message('required','El campo %s es obligatorio');
		$this->form_validation->set_message('is_unique', 'El campo %s ya esta registrado');
		$this->form_validation->set_message('max_length', 'El Campo %s debe tener un Maximo de %d Caracteres');
		$this->form_validation->set_message('min_length', 'El Campo %s debe tener un Minimo de %d Caracteres');
		$this->form_validation->set_message('alpha','El campo %s tiene caracteres incorrectos');
		$this->form_validation->set_message('alpha_dash','El campo %s tiene caracteres incorrectos');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		if($this->form_validation->run()==true){
		
	$imagen=$this->cargarImagen();
		

		$sede = array(
			'nombre' => $this->input->post('nomSede'),
			'direccion' => $this->input->post('dir'),
			'ubicacion' => $this->input->post('ubic'),
			'imagen' => $imagen['file_name'],
			'descripcion' => $this->input->post('decrip')

		);

		$this->load->model('administracion_model_sede');
		if ( $this->administracion_model_sede->insertar($sede) ){

 		redirect('administracionSede');
				
				
		}
			}
		else{
			
			$this->alta_sede();
		}	
	}
	public function inicio(){
		$this->load->view('inicio');
	}
	public function alta_sede(){
		parent::chequear();
		if (isset($this->session->userdata['privilegio'])) {
		
		$data['title'] = 'Alta de sede';
		$data['main_content'] = 'alta_sede';
		$this->load->view('main_template',$data);
	}
	}



	public function eliminar_sede(){
		$id = $this->uri->segment(3);
		$this->load->model('administracion_model_sede');
		if( $this->administracion_model_sede->eliminar($id) )
		 redirect('administracionSede');
		
	}
	
		
	public function modificar_sede_bd(){
		parent::chequear();
// 		$t=1;

		if (isset($this->session->userdata['privilegio'])) {
		$this->load->library('form_validation');
		$this->form_validation->set_rules('nomSede', 'Nombre de sede', 'required|min_length[3]|max_length[20]|alpha_dash|xss_clean');
		$this->form_validation->set_rules('dir', 'Direccion', 'required|min_length[3]');
		$this->form_validation->set_rules('ubic', 'Ubicacion', 'required|min_length[3]|integer');
//		$this->form_validation->set_rules('apellido', 'Apellido', 'required|min_length[3]|max_length[20]');
		$this->form_validation->set_rules('decrip', 'Descripcion', 'required|min_length[3]|max_length[20]|trim');
		
		$this->form_validation->set_message('integer', 'El campo %s debe poseer solo numeros enteros');
		$this->form_validation->set_message('required','El campo %s es obligatorio');
		$this->form_validation->set_message('is_unique', 'El campo %s ya esta registrado');
		$this->form_validation->set_message('max_length', 'El Campo %s debe tener un Maximo de %d Caracteres');
		$this->form_validation->set_message('min_length', 'El Campo %s debe tener un Minimo de %d Caracteres');
		$this->form_validation->set_message('alpha','El campo %s tiene caracteres incorrectos');
		$this->form_validation->set_message('alpha_dash','El campo %s tiene caracteres incorrectos');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$tipo=$this->input->post('tipo');
		
		
		if($this->form_validation->run()==true){
		
		
	
		$i=$this->cargarImagen();
			$imagen=$i['file_name'];
		if ($imagen ==""){

				$imagen=$this->input->post('image');
			
		
		}
		$sede = array(
				'nombre' => $this->input->post('nomSede'),
				'direccion' => $this->input->post('dir'),
				'ubicacion' => $this->input->post('ubic'),
				'imagen' =>$imagen,
				'descripcion' =>$this->input->post('decrip')
		);
		$id = $this->input->post('idsede');
		$this->load->model('administracion_model_sede');
		 $this->administracion_model_sede->actualiza_sede($id, $sede) ;
// 		 redirect('/administracionUsuario/administracionU');
		 
		if ($tipo=="modificarSede"){
			redirect('administracionSede');
			
		}
		elseif ($tipo=="perfilSede"){
			
			$nomSed=$this->input->post('nomSede');
			$this->session->set_userdata('nombresede', $nomSed);
			
			redirect('administracionSede/perfil_sede');
					
		
		}
		}
		else{
//			if ($tipo=="modificarSede"){
//		redirect('administracionSede/perfil_sede/');							
//		}
//		elseif ($tipo=="perfilSede"){
			if ($tipo=="perfilSede"){
			$this->perfil_sede();
								
			}
//		}
		}	
			
		
	}
	}

	public function modificar_sede(){

		parent::chequear();
// 		$t=1;

		if (isset($this->session->userdata['privilegio'])) {
			$data['title'] = 'Modificar sede';
			$data['main_content'] = 'modificar_sede';
			
			$this->load->model('administracion_model_sede');
			$id = $this->uri->segment(3);
			$data['sede'] = $this->administracion_model_sede->traer_sede($id);

			$this->load->view('main_template',$data);


//		}
		}

	}
	
	public function perfil_sede (){
			parent::chequear();
			if (isset($this->session->userdata['privilegio'])) {
			$this->load->library('session');
		
			$data['title'] = 'perfil sede';
			$data['main_content'] = 'perfil_sede';
			$id=$this->session->userdata['sede'];
			
			$this->load->model('administracion_model_sede');
			$data['sede'] = $this->administracion_model_sede->traer_sede($id);
			$this->load->view('main_template',$data);
		
	
			}
	}
	
	
	
}
